package com.wisdytech.linkdcs.account.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.account.dao.EquipDocumentDao;
import com.wisdytech.linkdcs.account.dao.TmDocumentDao;
import com.wisdytech.linkdcs.account.model.EquipDocument;
import com.wisdytech.linkdcs.account.model.TtEqDocument;
import com.wisdytech.linkdcs.account.service.ITmDocumentService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service("tmDocumentService")
@Transactional
public class TmDocumentServiceImpl extends BaseService<TmDocumentDao,TtEqDocument> implements ITmDocumentService {

    @Resource
    TmDocumentDao tmDocumentDao;
    @Resource
    EquipDocumentDao equipDocumentDao;
    @Override
    public PageInfo<Map<String, Object>> listDocumentByFilter(String equipCode, String equipName, String documentName, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(equipCode)) {
            params.put("equipCode",equipCode);
        }
        if(StringUtils.isNotBlank(equipName)) {
            params.put("equipName",equipName);
        }
        if(StringUtils.isNotBlank(documentName)) {
            params.put("documentName",documentName);
        }
        List<Map<String,Object>> list = this.beforeList(TmDocumentDao.class,filter).listDocumentByFilter(params);
        PageInfo<Map<String, Object>> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public EquipDocument getDocumentInfoById(String pkId) {
        if(StringUtils.isNotBlank(pkId)) {
        	EquipDocument documentInfo = equipDocumentDao.selectById(pkId);
            return documentInfo;
        }
        return null;
    }

	@Override
    public void saveOrUpdateDocument(TtEqDocument ttEqDocument, SysUser user) {
        //校验操作人
        if(user == null) {
            throw new ServiceException("操作人不能为空");
        }
        if(ttEqDocument !=null) {
            //检验文档编号
//            if(!StringUtils.isNotBlank(ttEqDocument.getDocumentCode())) {
//                throw new ServiceException("文档编号不能为空");
//            }
            //修改
            if(StringUtils.isNotBlank(ttEqDocument.getPkId())) {
                //校验文档编号是否重复
                EntityWrapper<TtEqDocument> ew =new EntityWrapper<>();
                ew.ne("PK_ID",ttEqDocument.getPkId());
                ew.eq("EQUIP_CODE",ttEqDocument.getEquipCode());
                ew.eq("DOCUMENT_NAME", ttEqDocument.getDocumentName());
                Integer count = tmDocumentDao.selectCount(ew);
                if(count>0) {
                    throw new ServiceException("文档名称重复");
                }
                //删除原文件
        		EntityWrapper<EquipDocument> ew1 = new EntityWrapper<>();
        		ew1.eq("EQUIP_DOCUMENT_ID", ttEqDocument.getPkId());
        		equipDocumentDao.delete(ew1);
        		//新增文件
        		List<EquipDocument> list = ttEqDocument.getFileList();
        		int totalSize = 0;
            	for(EquipDocument equipDocument : list) {
            		equipDocument.setPkId(BaseUtil.getUUID());
            		equipDocument.setEquipDocumentId(ttEqDocument.getPkId());
            		equipDocumentDao.insert(equipDocument);
            		String fileSize=  equipDocument.getFileSize();
            		totalSize = totalSize + Integer.parseInt(fileSize);
            	}
            	String size = String.valueOf(totalSize);
            	ttEqDocument.setTotalSize(size);
                ttEqDocument.setUpdateDate(new Date());
                ttEqDocument.setUpdateUser(user.getUsername());
                tmDocumentDao.updateById(ttEqDocument);
            } else {
            	String pkId = BaseUtil.getUUID();
                //校验文档编号是否重复
                EntityWrapper<TtEqDocument> ew =new EntityWrapper<>();
                ew.eq("EQUIP_CODE",ttEqDocument.getEquipCode());
                ew.eq("DOCUMENT_NAME", ttEqDocument.getDocumentName());
                Integer count = tmDocumentDao.selectCount(ew);
                if(count>0) {
                    throw new ServiceException("文档名称重复");
                }
        		List<EquipDocument> list = ttEqDocument.getFileList();
        		int totalSize = 0;
            	for(EquipDocument equipDocument : list) {
            		equipDocument.setPkId(BaseUtil.getUUID());
            		equipDocument.setEquipDocumentId(pkId);
            		equipDocumentDao.insert(equipDocument);
            		String fileSize=  equipDocument.getFileSize();
            		totalSize = totalSize + Integer.parseInt(fileSize);
            	}
            	String size = String.valueOf(totalSize);
            	ttEqDocument.setTotalSize(size);
                ttEqDocument.setDelFlag(CommonConstants.DEL_FLAG);
                ttEqDocument.setPkId(pkId);
                ttEqDocument.setUploadDate(new Date());
                ttEqDocument.setCreateDate(new Date());
                ttEqDocument.setCreateUser(user.getUsername());
                tmDocumentDao.insert(ttEqDocument);
            }
        }
    }

    @Override
    public void deleteDocument(String pkIds) {
        List<String> idList = new ArrayList<String>();
        String[] id = pkIds.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        //删除原文件
        for(String pkId : idList) {
    		EntityWrapper<EquipDocument> ew1 = new EntityWrapper<>();
    		ew1.eq("EQUIP_DOCUMENT_ID", pkId);
    		equipDocumentDao.delete(ew1);
        }
        tmDocumentDao.deleteBatchIds(idList);
    }

	@Override
	public PageInfo<EquipDocument> listFileByFilter(String fileId, Filter filter) {
		EntityWrapper<EquipDocument> ew = new EntityWrapper<>();
		ew.eq("EQUIP_DOCUMENT_ID", fileId);
		List<EquipDocument> list = this.beforeList(EquipDocumentDao.class,filter).selectList(ew);
		PageInfo<EquipDocument> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public Map<String, Object> getDocumentInById(String pkId) {
		Map<String, Object> document= tmDocumentDao.getDocumentInfoById(pkId);
		return document;
	}

	@Override
	public void filedel(String pkId) {
		equipDocumentDao.deleteById(pkId);
	}
}
